function  [S] = expandFromPoint3( inputFileName,sliceNumber,outputFileName)


%load the nifti file and extract the relevant slice
s = load_untouch_nii(inputFileName);
mat = s.img;

imshow(mat(:,:,sliceNumber),[min(min(mat(:,:,sliceNumber))) max(max(mat(:,:,sliceNumber)))],'InitialMagnification',150);


msize=size(mat);
% instlaize the output matrix %
outMat = zeros(msize(1), msize(2), msize(3));

wasThere = zeros(msize(1), msize(2), msize(3));
% read the click %
[y,x]=ginput(1);
z = sliceNumber;
x=round(x);
y=round(y);

maxGray = 1.1*mat(x,y,z);
minGray = 0.9*mat(x,y,z);
% matrix that used as a queue for the places that need to be checked %
toScan = zeros(1,3);
toScan(1,1)=x;
toScan(1,2)=y;
toScan(1,3)=z;
outMat(x,y,z)=1;
scanSize = size(toScan);
while(scanSize(1) > 0)
    x=round(toScan(1,1));
    y=round(toScan(1,2));
    z=round(toScan(1,3));
    toScan(1,:)=[];
    
    %  check the neighbors of this point %
    if(wasThere(x,y,z)==0)
        wasThere(x,y,z)=1;
        for i = x-1 : x+1
            if i>0 && i < 256
                for j = y-1 : y+1
                    if j>0 && j < 256
                        for k = z-1 : z+1
                            if k >0 && k < 26
                                grayVal = mat(i,j,k);
                                if(grayVal >= minGray && grayVal <= maxGray)
                                    outMat(i,j,k)=1;
                                   
                                    %  add this neighbor to the toSczn list%
                                    add=[i,j,k];
                                    toScan=[toScan ; add];
                                end
                            end
                        end
                    end
                end
            end
        end
    end
    scanSize = size(toScan);
end
close (1);
%save_nifti(outMat, outputFileName, 2);

s.img = outMat;
save_untouch_nii(s,outputFileName);
S = outMat;
end
